package cn.yitun.chat.yitunservermanagecore.general.manage.core.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.common.SyslogDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.common.SyslogVo;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.core.service.SysLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统日志
 * 
 * @author author
 * @email chenyitun@163.com
 * @date 2020-02-08 16:23:50
 */
 
@RestController
@RequestMapping("/manage/core")
public class LogController extends BaseController {

	@Autowired
	private SysLogService logService;

	/**
	 * 列表 分页
	 * @param SyslogDto
	 * @return
	 */
	@RequestParameter
	@PermissionMapping(name = "列表", perms = "sys:log:list")
    @PostMapping("/syslog/page")
    public ResultMessage list(JwtUserDto jwtUserDto, @JsonDefine SyslogDto SyslogDto){
        //查询列表数据
		checkRootPage(SyslogDto, jwtUserDto);
		PageHelper.startPage(SyslogDto.getCurrentPageNo() , SyslogDto.getPageSize());
		List<SyslogVo> list = logService.listVO(SyslogDto);
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("page", new PageInfo<>(list));
        return rm;
    }


	/**
	 * 删除
	 */
	@RequestParameter
	@PermissionMapping(name = "删除", perms = "sys:log:remove")
	@DeleteMapping( "/syslog/remove/{id}")
	public ResultMessage remove( @PathVariable("id") Long id){
		if( logService.remove(id) == 0 )
            throw new YitunException(ExceptionEnum.DELETE_FAILED);

		return new ResultMessage();
	}
	
	/**
	 * 批量删除
	 */
	@RequestParameter
	@PermissionMapping(name = "批量删除", perms = "sys:log:remove")
	@DeleteMapping( "/syslog/batchRemove/{ids}")
	public ResultMessage batchRemove(@PathVariable("ids") Long[] ids){
        if( logService.batchRemove(ids) == 0 )
            throw new YitunException(ExceptionEnum.DELETE_FAILED) ;

		return new ResultMessage();
	}

}
